package Compation;

import java.util.HashMap;
import java.util.Map;

public class findLucky {
    Map<Integer, Integer> temp = new HashMap<>();

    public int findLucky(int[] arr) {
        if (arr == null) {
            return -1;
        }
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            temp.put(arr[i], temp.getOrDefault(arr[i], 0) + 1);
        }
        int max = -1;
        for (int key: temp.keySet()) {
            if (key == temp.get(key)) {
                if (key > max) {
                    max = key;
                }
            }
        }
        return max;
    }
}
